package com.boomsecret.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/7/17.
 */
public class HttpUtils {
    private static final String Tag = HttpUtils.class.getSimpleName();

    /**
     * GET
     * @param targetUrl
     * @return
     */
    public static String get(String targetUrl) {
        LogUtils.d(Tag, "RequestContent:" + targetUrl);
        HttpURLConnection connection = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            URL url = new URL(targetUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
            if(connection.getResponseCode() == 200){
                InputStream inputStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            }
        } catch (IOException e) {
            LogUtils.d(Tag, "Exception:" + e.getMessage());
        } finally {
            if(connection != null){
                connection.disconnect();
            }
            LogUtils.d(Tag, "ResponseContent:" + stringBuilder.toString());
            return stringBuilder.toString();
        }
    }

    /**
     * POST
     * @param targetUrl
     * @param params key=value&.....
     * @return
     */
    public static String post(String targetUrl, String params) {
        LogUtils.d(Tag, "RequestUrl:" + targetUrl);
        LogUtils.d(Tag, "RequestContent:" + params);
        HttpURLConnection connection = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            URL url = new URL(targetUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setUseCaches(false);
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("Charset", "UTF-8");
            httpConn.connect();
            DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
            dos.writeBytes(params);
            dos.flush();
            dos.close();
            int resultCode = httpConn.getResponseCode();
            if(HttpURLConnection.HTTP_OK == resultCode){
                String readLine = null;
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
                while((readLine = responseReader.readLine()) != null){
                    stringBuilder.append(readLine);
                }
                responseReader.close();
            }
        } catch (Exception e) {
            LogUtils.d(Tag, "Exception:" + e.getMessage());
        } finally {
            if(connection != null){
                connection.disconnect();
            }
            LogUtils.d(Tag, "ResponseContent:" + stringBuilder.toString());
            return stringBuilder.toString();
        }
    }
}
